﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Xml;
using System.Xml.Linq;

namespace MockingBird.Simulator.Framework.Configuration
{
    [Serializable]
    public class HandlerRegistrationInfo
    {
        private List<HandlerRegistrationEntry> entries;

        public bool IsEmpty
        {
            get
            {
                if (this.entries.Count == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public HandlerRegistrationInfo()
        {
            Debug.WriteLine(string.Format("HandlerRegistrationInfo.ctor on thread ID: {0}", Thread.CurrentThread.ManagedThreadId));
            this.entries = new List<HandlerRegistrationEntry>();
        }

        public List<HandlerRegistrationEntry> GetEntries()
        {
            return this.entries;
        }

        public void LoadInfoFromXml(string xml)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.LoadXml(xml);
            this.LoadInfoFromXmlDocument(xmlDocument);
        }

        public void LoadInfoFromXmlDocument(XmlDocument xmlDocument)
        {
            this.entries = new List<HandlerRegistrationEntry>();

            XmlNodeList nodeList = xmlDocument.SelectNodes("//RegistrationEntry");
            if (nodeList != null)
            {
                foreach (XmlNode node in nodeList)
                {
                    HandlerRegistrationEntry newEntry = new HandlerRegistrationEntry();
                    newEntry.HandlerName = node.Attributes["handlerName"].InnerText;
                    newEntry.FullTypeName = node.Attributes["fullTypeName"].InnerText;
                    if (!this.EntryExists(newEntry))
                    {
                        this.entries.Add(newEntry);
                    }

                }
            }
        }

        public void AddEntry(HandlerRegistrationEntry newEntry)
        {
            if (!this.EntryExists(newEntry))
            {
                this.entries.Add(newEntry);
            }
        }

        public string GetHandlerTypeName(string handlerName)
        {
            return this.entries.Find(x => x.HandlerName.Equals(handlerName)).FullTypeName;
        }

        public string Serialize()
        {
            return this.SerializeToXElement().ToString();

        }

        public bool EntryExists(HandlerRegistrationEntry candidate)
        {
            return this.entries.Exists(e => e.Equals(candidate));
        }

        public XElement SerializeToXElement()
        {
            XElement registrationCollectionElement = new XElement("HandlerRegistration");
            foreach (HandlerRegistrationEntry entry in this.entries)
            {
                XElement entryElement = new XElement("RegistrationEntry");
                XAttribute handlerNameAttribute = new XAttribute("handlerName", entry.HandlerName);
                XAttribute fullTypeNameAttribute = new XAttribute("fullTypeName", entry.FullTypeName);
                entryElement.Add(handlerNameAttribute, fullTypeNameAttribute);
                registrationCollectionElement.Add(entryElement);
            }

            return registrationCollectionElement;

        }
    }
}
